<?php
namespace app\common\controller;
use think\Controller;
use think\Request;

class Base extends Controller{
    use Task,Uploads,Navs,Auth,Mine;
    protected $table='';
	function __construct(){
        parent::__construct();
        // if(config('app_debug')){
        //     header('Access-Control-Allow-Origin:*');
        // }
        
        $this->table=request()->controller();
    }
    // 文件上传提交入口
    function loads(){
        $file=request()->file('file');
        //return json(lang('Upload failure'),1);
        if($file){
            $rs=$this->uploads($file);
            if(is_string($rs)){
                return json($rs,1);
            }
            return json(lang('Upload success'),0,$rs);
            
        }
    }
    function getDevicesTree($ids=[],$get=false){
        if($get||request()->isAjax()||request()->isPost()){
            $devsTree=model('device')->tree($this->user['id'],$ids);
            if($get){
                return $devsTree;
            }
            $this->success('','',$devsTree);
        }
    }
    function _empty(){
    	return '<h1 style="font-size:220px;line-height:500px;text-align:center">404</h1>';
    }
    /**
     * 地址转经纬度（百度地图接口）
     * @param  string $address 地址 西乡塘区友爱北路65-8号
     * @return array  ['lng','lat']
     */
    protected function getPoint($address=''){
        if(empty($address))return [];
        //$curl="http://api.map.baidu.com/geocoder/v2/?address=$address&output=json&ak=".config('setting.baidu_ak');
        //$curl="http://restapi.amap.com/v3/geocode/geo?address=$address&key=".config('setting.gaode_ak');
        $curl="http://apis.map.qq.com/ws/geocoder/v1/?address=$address&key=".config('setting.qqmap_ak');
        $rs=_curl($curl,false);
        $rs=json_decode($rs,true);
        $location=$rs['result']['location'];// 百度地图 腾讯地图
        //高德地图
        // $geocodes=explode(',', $rs['geocodes'][0]['location']);
        // $location=[
        //     'lng'=>$geocodes[0],
        //     'lat'=>$geocodes[1],
        // ];
        return $location;
    }
    /**
     * 获取城市ID
     * @param string  $city  城市名称
     * @param boolean $isCity  true:city false:area
     */
    protected function getCityId($city='',$isCity=true){
    	if(empty($city))return 0;
        $table=$isCity?'city':'area';
    	$rs=model($table)->read(['name'=>$city],'id');
    	if(empty($rs)){
    		$rs=model('area')->add(['name'=>$city]);
    	}
    	return isset($rs['id'])?$rs['id']:0;
    }
    /**
     * 验证是否超级管理员
     * @param  boolean $returnJson [description]
     * @return [type]              [description]
     */
    protected function superCheck($returnType='view'){
        if($this->user['id']!==config('auth.super_id')){
            $msg='当前仅允许超级管理员进行该操作';
            switch ($returnType) {
                case 'json':
                    return json($msg,1);
                    break;
                case 'array':
                    return error($msg);
                    break;
                
                default:
                    $this->error($msg);
                    break;
            }
        }
    }
    /**
     * 关闭、启用
     * @param  string  $table 操作表名
     * @param  integer $id    ID
     * @param  array   $ids   ID组（多项操作）
     * @param  string  $field 字段 默认：'status'
     * @return json
     */
    protected function _close($table,$id=0,$ids=[],$field='status'){
        if($id<=0&&empty($ids)){
            return error(lang('Data does not exist'));
        }
        $where="";
        if(!empty($ids)){
            $ids=implode(',', $ids);
            $where=" `id` in ({$ids}) ";
        }else{
            $where=" `id`={$id} ";
        }
        $rs=model($table)->close($where,$field,true);
        if($rs){
            return success(lang('Successful operation'));
        }
        return error(lang('Failed operation'));
    }
    
}
